<?php

/**
 * Created by PhpStorm.
 * User: xiaoqiang
 * Date: 15-3-23
 * Time: 上午11:52
 */
class LibLog
{
    static private $tableName = "lib_log";

    /**
     * DB日志
     * @param $string
     * @param string $project
     * @param string $category
     * @return bool|int|string
     */
    static private function _writeDB($string, $project = "", $category = "")
    {
        $db = \LibFactory::database('base');
        $datas = [
            'project' => $project,
            'category' => $category,
            'content' => $string,
            'dateline' => time()
        ];
        return $db->insert(self::$tableName, $datas);
    }

    /**
     * 文件日志
     * @param $string
     * @param bool $print
     * @return bool
     */
    static private function _writeFile($string, $print = true)
    {
        $logFile = \LibConfig::readLog();
        file_put_contents(SYS_LOG . $logFile, date("Y-m-d H:i:s", time()) . "\t" . $string . "\r\n", FILE_APPEND);
        //$print && print_r($string);
        //exit();
    }

    /**
     * 普通方式写日志(写入文件)
     * @param $string
     * @return bool
     */
    static function custom($string)
    {
        if (\LibFunction::isSinaSae()) {
            return self::toDb($string, "file");
        }
        return self::_writeFile($string);
    }

    /**
     * 永久日志(写入DB)
     * @param string $string
     * @param string $project
     * @param string $category
     * @return bool|int|string
     */
    static function toDb($string = "", $project = "", $category = "")
    {
        if (!$string) return false;
        return self::_writeDB($string, $project, $category);
    }
}